<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script type="text/javascript">
    window._AMapSecurityConfig = {
      // securityJsCode: 'c5b3373591dcf4cc8c2645',
    }
  </script>
  <script
    type="text/javascript"
    src="https://webapi.amap.com/maps?v=2.0&key=9459c43cfd26752214a04ca5de3c7db5"
  ></script>
  <style>
    #container {
      margin: auto;
      width: 800px;
      height: 500px;
    }
  </style>
  <body>
    <div id="container"></div>
    <div class="input-card" style="width: auto">
      <div class="input-item">
        <button class="btn" onclick="toggle()">显示/隐藏实时路况</button>
      </div>
    </div>
  </body>
  <script>
    var map = new AMap.Map('container', {
      zoom: 16, //级别
      center: [113.6249, 34.7472], //中心点坐标
      viewMode: '3D', //使用3D视图
    })
    //实时路况图层
    var trafficLayer = new AMap.TileLayer.Traffic({
      zIndex: 10,
    })

    trafficLayer.setMap(map)

    var isVisible = true
    function toggle() {
      if (isVisible) {
        trafficLayer.hide()
        isVisible = false
      } else {
        trafficLayer.show()
        isVisible = true
      }
    }
  </script>
</html>
